let wrap = document.querySelector(".wrap");
let nav = document.querySelector("nav");
let container = document.querySelector(".container");
let tbody = document.querySelector("tbody");

let list = [];
let flag = true;
let index = 0;

wrap.onclick = function (e) {
  e = e || window.event;
  let target = e.target;

  // 导航栏按钮
  if (target.className == "navBtn") {
    if (flag) {
      wrap.style.transform = "translate(15%)";
      flag = false;
    } else {
      wrap.style.transform = "translate(0)";
      flag = true;
    }
  }

  // 添加用户
  if (target.className == "add") {
    if (index >= dataInfo.length) return;
    list.push(dataInfo[index]);
    index++;
  }

  // 资金翻倍
  if (target.className == "double") {
    list.forEach(function (item) {
      item.money = item.money * 2;
    });
  }

  // 查询百万富翁
  if (target.className == "million") {
    list = list.filter(function (item) {
      return item.money >= 1000000;
    });
  }

  // 财富榜
  if (target.className == "wealth") {
    list.sort(function (a, b) {
      return a.money - b.money;
    });
  }

  // 总金额
  if (target.className == "total") {
    let sum = list.reduce(function (prev, item) {
      return prev + item.money;
    }, 0);
    alert(`总计金额为：$ ${sum.toLocaleString()}`);
  }

  bindList();
};

// 数据渲染函数
function bindList() {
  let str = "";
  list.forEach(function (item) {
    str += `
  <tr>
  <td>${item.name}</td>
  <td>$ ${item.money.toLocaleString()}</td>
  </tr>
 `;
  });
  tbody.innerHTML = str;
}
